package entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import utils.OrderSubmitException;

public class Cart {
    
    private User user;
    private Order order;
    
    public Cart(User user){
        this.user = user;
        resetOrder();
    }
    
    private void resetOrder() {
        order = new Order();
        order.setUser(user);
    }
    
    public void submitOrder() throws OrderSubmitException {
        order.submit();
        
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("vh11i_jeePU");
        EntityManager em = fact.createEntityManager();
        
        try {
            em.getTransaction().begin();
            em.persist(order);
            em.getTransaction().commit();
        }
        finally {
            em.close();
            fact.close();
        }
        
        resetOrder();
    }
    
    public Order getOrder() {
        return order;
    }
    
}
